package cn.player;

import android.app.Application;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;

import org.json.JSONObject;

import java.util.Map;

import cntv.player.core.util.SprefUtils;
import cntv.player.core.util.Utils;
import cntv.player.engine.Const;
import cn.player.media.entity.AdUrls;
import cn.player.media.entity.BaseUrl;
import cn.player.media.util.ParseUtil;

public class App extends Application {

    //06-30 13:38:30.151: I/cctv(9029): IntentService[appId]:AfmobiAidlService :
    //Through aidl obtains clientId => 1a87fa6b32ef4b40aefff1e1ba576237b1ed5e44197f4d16a026706562cb7dc7

    //	public static final String APPID = "1a87fa6b32ef4b40aefff1e1ba576237";//央视网正式推送ID
    public static final String APPID = "46bd6a1d4b834c2d8060838c85387737";//推送测试id for testmd

    public static boolean isTimeShift = true;

    public Map<Object, BaseUrl> baseUrls;
    public AdUrls adUrl;

    public FinalHttp finalHttp;
    public FinalBitmap finalBitmap;
    public FinalDb finalDb;
    public static final String DB_NAME = "cntvPlayDb.db";
//	public Config config;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
//		config = Config.getInstance();
//		config.setContext(this);
        if (finalHttp == null) {
            finalHttp = new FinalHttp();
        }
        if (finalBitmap == null) {
            finalBitmap = FinalBitmap.create(this);
            if (Utils.getCachePath(this) != null) {
                finalBitmap.configDiskCachePath(Utils.getCachePath(this));
            }
        }
        if (finalDb == null) {
            finalDb = FinalDb.create(this, DB_NAME);
        }
    }

    public FinalHttp getFinalHttp() {
        if (finalHttp == null) {
            finalHttp = new FinalHttp();
        }
        return finalHttp;
    }

    public FinalBitmap getFinalBitmap() {
        if (finalHttp == null) {
            finalBitmap = FinalBitmap.create(this);
            if (Utils.getCachePath(this) != null) {
                finalBitmap.configDiskCachePath(Utils.getCachePath(this));
            }
        }
        return finalBitmap;
    }

    public FinalDb getFinalDb() {
        if (finalDb == null) {
            finalDb = FinalDb.create(this, DB_NAME);
        }
        return finalDb;
    }

    public Map<Object, BaseUrl> getBaseUrls() {
        return baseUrls == null ? getCacheBaseUrls() : baseUrls;
    }

    public void setBaseUrls(Map<Object, BaseUrl> baseUrls) {
        this.baseUrls = baseUrls;
    }

    /**
     * 从shared_pref中获取主配置地址信息
     *
     * @return baseUrlsMap
     */
    public Map<Object, BaseUrl> getCacheBaseUrls() {
        Map<Object, BaseUrl> baseUrlsMap = null;
        try {
            String jsonString = SprefUtils.getInstance(this).getString(Const.SHAR_PREF_KEY_CONFIG);
            if (jsonString != null && !"".equals(jsonString)) {
                JSONObject jsonbject = new JSONObject(jsonString);
                baseUrlsMap = ParseUtil.parseDataToMap(jsonbject, "data", "title", BaseUrl.class);

                this.setBaseUrls(baseUrlsMap);//如果从缓存获取成功后，将数据存于内存中
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return baseUrlsMap;
    }
}
